<!doctype html>
<meta charset=utf-8>
<title>IndexedDB: Test IDBObjectStore.getKey()</title>
<meta name=timeout content=long>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support.js"></script>
<script>

function getkey_test(func, name) {
    indexeddb_test(
        function(t, db, tx) {
            var basic = db.createObjectStore('basic');
            var key_path_store = db.createObjectStore('key path',
                {keyPath: 'id'});
            var key_generator_store = db.createObjectStore('key generator',
                {autoIncrement: true});
            var key_generator_and_path_store = db.createObjectStore(
                'key generator and key path',
                {autoIncrement: true, key_path: 'id'});

            for (var i = 1; i <= 10; ++i) {
                basic.put('value: ' + i, i);
                key_path_store.put({id: i});
                key_generator_store.put('value: ' + i);
                key_generator_and_path_store.put({});
            }
        },
        func,
        name
    );
}

getkey_test(function(t, db) {
    var tx = db.transaction('basic');
    var store = tx.objectStore('basic');
    assert_throws_js(TypeError, function() { store.getKey(); });
    assert_throws_dom('DataError', function() { store.getKey(null); });
    assert_throws_dom('DataError', function() { store.getKey({}); });
    t.done();
}, 'IDBObjectStore.getKey() - invalid parameters');

[
    'basic',
    'key path',
    'key generator',
    'key generator and key path'
].forEach(function(store_name) {

    getkey_test(function(t, db) {
        var tx = db.transaction(store_name);
        var store = tx.objectStore(store_name);
        var request = store.getKey(5);
        request.onerror = t.unreached_func('request failed');
        request.onsuccess = t.step_func(function() {
            assert_equals(request.result, 5);
        });
        tx.onabort = t.unreached_func('transaction aborted');
        tx.oncomplete = t.step_func(function() { t.done(); });
    }, 'IDBObjectStore.getKey() - ' + store_name + ' - key');

    getkey_test(function(t, db) {
        var tx = db.transaction(store_name);
        var store = tx.objectStore(store_name);
        var request = store.getKey(IDBKeyRange.lowerBound(4.5));
        request.onerror = t.unreached_func('request failed');
        request.onsuccess = t.step_func(function() {
            assert_equals(request.result, 5);
        });
        tx.onabort = t.unreached_func('transaction aborted');
        tx.oncomplete = t.step_func(function() { t.done(); });
    }, 'IDBObjectStore.getKey() - ' + store_name + ' - range');

    getkey_test(function(t, db) {
        var tx = db.transaction(store_name);
        var store = tx.objectStore(store_name);
        var request = store.getKey(11);
        request.onerror = t.unreached_func('request failed');
        request.onsuccess = t.step_func(function() {
            assert_equals(request.result, undefined);
        });
        tx.onabort = t.unreached_func('transaction aborted');
        tx.oncomplete = t.step_func(function() { t.done(); });
    }, 'IDBObjectStore.getKey() - ' + store_name + ' - key - no match');

    getkey_test(function(t, db) {
        var tx = db.transaction(store_name);
        var store = tx.objectStore(store_name);
        var request = store.getKey(IDBKeyRange.lowerBound(11));
        request.onerror = t.unreached_func('request failed');
        request.onsuccess = t.step_func(function() {
            assert_equals(request.result, undefined);
        });
        tx.onabort = t.unreached_func('transaction aborted');
        tx.oncomplete = t.step_func(function() { t.done(); });
    }, 'IDBObjectStore.getKey() - ' + store_name + ' - range - no match');
});

</script>
